package com.sudo.service.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.constant.DictConstant;
import com.sudo.common.exception.BizException;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.dao.admin.entity.SysDict;
import com.sudo.dao.admin.mapper.SysDictMapper;
import com.sudo.dao.admin.model.bo.*;
import com.sudo.service.admin.service.AdminBaseService;
import com.sudo.service.admin.service.SysDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 为道日损 2020-06-23 16:49
 * webSite：https://www.sudo.ren
 */
@Service
public class SysDictServiceImpl extends AdminBaseService implements SysDictService {

    @Resource
    private SysDictMapper sysDictMapper;

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    @Override
    public int addDict(SysDictAddBO sysDictAddBO) {
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictAddBO,sysDict);
        sysDict.setParentCode(DictConstant.DICT_P_CODE);
        return sysDictMapper.insertSelective(sysDict);
    }

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    @Override
    public int addDictSub(SysDictSubAddBO sysDictSubAddBO) {
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictSubAddBO,sysDict);
        return sysDictMapper.insertSelective(sysDict);
    }

    @Transactional(propagation = Propagation.SUPPORTS ,rollbackFor = Exception.class)
    @Override
    public SysDict getSysDictByCode(String dictCode) {
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(SysDict.Field.dictCode, dictCode);
        return sysDictMapper.selectOneByExample(example);
    }

    @Transactional(propagation = Propagation.SUPPORTS ,rollbackFor = Exception.class)
    @Override
    public PagedGridResult getParentDictByPage(SysDictQueryBO sysDictQueryBO) {
        PageHelper.startPage(sysDictQueryBO.getPage(), sysDictQueryBO.getLimit());
        Example example = new Example(SysDict.class);
        example.orderBy(SysDict.Field.id).desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(SysDict.Field.parentCode, DictConstant.DICT_P_CODE);
        if (StringUtils.isNotBlank(sysDictQueryBO.getDictName())){
            criteria.andLike(SysDict.Field.dictName, "%" + sysDictQueryBO.getDictName() + "%");
        }
        if (StringUtils.isNotBlank(sysDictQueryBO.getDictCode())){
            criteria.andEqualTo(SysDict.Field.dictCode, sysDictQueryBO.getDictCode());
        }

        List<SysDict> dictList = sysDictMapper.selectByExample(example);
        return setterPagedGrid(dictList ,sysDictQueryBO.getPage());
    }

    @Transactional(propagation = Propagation.SUPPORTS ,rollbackFor = Exception.class)
    @Override
    public PagedGridResult getChildDictByPCode(SysSubDictQueryBO sysSubDictQueryBO) {
        PageHelper.startPage(sysSubDictQueryBO.getPage(), sysSubDictQueryBO.getLimit());
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(SysDict.Field.parentCode, sysSubDictQueryBO.getParentCode());
        List<SysDict> sysDictList = sysDictMapper.selectByExample(example);
        return setterPagedGrid(sysDictList ,sysSubDictQueryBO.getPage());
    }

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    @Override
    public int updateDictByDictCode(SysDictUpdateBO sysDictUpdateBO) {
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(SysDict.Field.dictCode, sysDictUpdateBO.getDictCode());
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictUpdateBO,sysDict, SysDict.Field.dictCode);
        return sysDictMapper.updateByExampleSelective(sysDict,example);
    }

    @Transactional(propagation = Propagation.REQUIRED ,rollbackFor = Exception.class)
    @Override
    public int deleteDictByCode(String dictCode) {
        //查询子级字典
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(SysDict.Field.parentCode, dictCode);
        int count = sysDictMapper.selectCountByExample(example);
        if (count > 0){
            throw new BizException("不能删除含有子级字典的父级字典");
        }

        SysDict sysDict = new SysDict();
        sysDict.setDictCode(dictCode);
        return sysDictMapper.delete(sysDict);
    }


}
